Ext.define('AM.view.AddWorkView', {
		extend: 'Ext.grid.Panel',
		alias : 'widget.addWorkView',
//			initComponent: function(){
//		Ext.apply(this,{
		store : 'AddWorkStore',
		columnLines:true,
//		selModel : Ext.create('Ext.selection.CheckboxModel'), 
		selModel: {
	        injectCheckbox: 0,
	        mode: "SIMPLE"  //"SINGLE"/"SIMPLE"/"MULTI"
//	        checkOnly: true     //只能通过checkbox选择
   		 },
   		selType: "checkboxmodel",
    	dockedItems: [{
        xtype: 'pagingtoolbar',
        dock: 'bottom',
        store : 'AddWorkStore',
        displayInfo: true
    }],
    plugins: [{
            ptype: 'rowexpander',
            rowBodyTpl : new Ext.XTemplate(
                '<p><b>项目名称:</b> {name}</p>',
                '<p><b>工作内容:</b> {content}</p>',
                '<p><b>审批意见:</b> {advice}</p><br>'  
            )
        }],
		columns: [	
			        {text: '序号', xtype: 'rownumberer',width:50,align:'center'},
			        { text: 'ID', dataIndex: 'id', width:0,align:'center'},
			        { text: '项目名称', dataIndex: 'name', width:100,align:'center'},
			        { text: '科室', dataIndex: 'department', width:100,align:'center'},
			        { text: '申请人', dataIndex: 'employee', width:100,align:'center'},
			        { text: '开始时间', dataIndex: 'starttime', width:160,align:'center',xtype: 'datecolumn',format:'Y-m-d'},
			        { text: '结束时间', dataIndex: 'endtime', width:160,align:'center',xtype: 'datecolumn',format:'Y-m-d'},
			        { text: '工作内容', dataIndex: 'content', width:160,align:'center'},
			        { text: '状态', dataIndex: 'state', width:80,align:'center',renderer: function(value){
					        	 if (value === 0) {
										 	return '<span>待审批</span>';
								 }if (value === 1) {
										return '<span style="color:green;">审批通过</span>';
								 }if (value === 2) {
										return '<span style="color:red;">审批未通过</span>';
								 }}
			        },
			        { text: '审批意见', dataIndex: 'advice', width:200,align:'center'}
			     ],
			   tbar: [
					  { xtype: 'button', text: '申请',handler: applywork,icon: 'Ext/restful/images/add.png'
					  },
					  { xtype: 'button', text: '修改',handler: editWork,icon: 'Ext/restful/images/cog_edit.png'
					  },
					  { xtype: 'button', text: '删除',handler: deleteWork,icon: 'Ext/restful/images/delete.png'
					  }
					]
//											});
//			   this.callParent(arguments);
//  	}
		
})
	function applywork() {
		 var grid=this.ownerCt.ownerCt;
		 var myform1= new Ext.form.Panel({
			        layout : "form",
			        labelWidth : 65,
			        items: [{
			        			layout : "column",
					       		border : false,
					        	items: [{
					        		        columnWidth :.70,
											layout : "form",
											border : false,
								   			items:[{
								   				    xtype : "textfield",
								   				    fieldLabel: '项目名称',
								   				    labelAlign :"right",
										            name: 'name',
										            allowBlank:false
								   			}]
					        	}]
			       			 },
//			       			 	{
//					        	layout : "column",
//					       		border : false,
//					        	items: [{
//								        	columnWidth :.40,
//											layout : "form",
//											border : false,
//								   			items:[{
//												       xtype : "combobox",
//												       allowBlank:false,
//												       emptyText:'请选择科室',
//												       selectOnFocus:true,
//													   forceSelection: true,
//													   editable:false,
//													   queryMode: 'local',
//												       labelAlign :"right",
//												       fieldLabel: '科室',
//												       name: 'department.id',
//												       store :"TargetDepartStore",
//												       displayField: 'name',
//			    									   valueField: 'id',
//												       listeners:{   
//																select:function(combo, record,index){
//																		try{
//																			  var parent=Ext.getCmp('employee_11');
//																			  parent.clearValue();
//																			  parent.store.load({params:{param1:this.value}});
//																		 }
//																		 catch(ex){
//																			 Ext.MessageBox.alert("错误","数据加载失败。");
//																			 }
//																		}
//																	}
//												        }]
//					        		},{
//				        			    columnWidth :.40,
//										layout : "form",
//										border : false,
//							   			items:[{
//											       xtype : "combobox",
//											       allowBlank:false,
//											       emptyText:'请选人员',
//											       selectOnFocus:true,
//												   forceSelection: true,
//												   editable:false,
//												   queryMode: 'local',
//											       labelAlign :"right",
//											       id:'employee_11',
//											       fieldLabel: '人员',
//											       name: 'employee.id',
//											       store :"EmployeesStore",
//											       displayField: 'name',
//		    									   valueField: 'id'
//							   			}]
//					        		}]	
//					        	},
					        		{
							         layout : "column",
							         border : false,			 
								     items: [{
									        	columnWidth :.40,
												layout : "form",
												border : false,
									   			items: [{
										   				    xtype : 'datefield',
										   				    labelAlign :"right",
										   				    fieldLabel: '开始时间',
												            name: 'starttime',
												            format:'Y-m-d'
											        }]		        	
								        	},{
								        		columnWidth :.40,
												layout : "form",
												border : false,
									   			items: [{
										   				    xtype : 'datefield',
										   				    labelAlign :"right",
										   				    fieldLabel: '结束时间',
												            name: 'endtime',
												            format:'Y-m-d'
												            
											        }]	
								        		
								        	}]
							  },{
					   		 	layout : "column",
							 	border : false,
									items: [{
											 columnWidth :.80,
									 		 layout : "form",
											 xtype : "textarea",
									      	 labelAlign :"right",
									         fieldLabel: '项目内容',
									         name: 'content'
									}]
			   		 }]
		 })
			var win = new Ext.Window({
				title : "申请临时性工作",
				width : 700,
				height : 300,
				frame:true,
				layout:'fit',
				items : [ myform1
							],
				buttons : [{
							text : "确定",
							handler : function() {
								win.getComponent(0).getForm().submit({
											    url : 'AddWork_addAddWork',
											    success : function() {
											    Ext.Msg.alert('提示', '添加成功');
											    win.close();
											    grid.getStore().reload();
											},
											failure : function() {
												Ext.Msg.alert('错误', '添加失败');
											},
											waitMsg : "请稍后，正在保存。。。。"
	
										});
							}
						}, {
							text : "重置",
							handler : function() {
								win.getComponent(0).getForm().reset();
							}
						}, {
							text : "取消",
							handler : function() {
												win.close();
											
							}
						}]
			});
		win.show();
	}
	
	function editWork() {
		 var grid=this.ownerCt.ownerCt;
		 var myform1= new Ext.form.Panel({
			        
			        labelWidth : 65,
			        items: [{
					        	layout : "column",
							       		border : false,
							        	items: [{
							        		        columnWidth :.70,
													layout : "form",
													border : false,
										   			items:[{
										   				    xtype : "textfield",
										   				    fieldLabel: '项目名称',
										   				    labelAlign :"right",
												            name: 'name',
												            allowBlank:false
										   			}]
							        	}]
			       		 },
//			       		 	{
//					        	layout : "column",
//					       		border : false,
//					        	items: [{
//								        	columnWidth :.45,
//											layout : "form",
//											border : false,
//								   			items:[{
//												       xtype : "combobox",
//												       allowBlank:false,
//												       emptyText:'请选择科室',
//												       selectOnFocus:true,
//													   forceSelection: true,
//													   editable:false,
//													   queryMode: 'local',
//												       labelAlign :"right",
//												       fieldLabel: '科室',
//												       name: 'department',
//												       store :"TargetDepartStore",
//												       displayField: 'name',
//			    									   valueField: 'id',
//												       listeners:{   
//																select:function(combo, record,index){
//																		try{
//																			  var parent=Ext.getCmp('employee_12');
//																			  parent.clearValue();
//																			  parent.store.load({params:{param1:this.value}});
//																		 }
//																		 catch(ex){
//																			 Ext.MessageBox.alert("错误","数据加载失败。");
//																			 }
//																		}
//																	}
//												        }]
//					        		},{
//					        			    columnWidth :.45,
//											layout : "form",
//											border : false,
//								   			items:[{
//												       xtype : "combobox",
//												       allowBlank:false,
//												       emptyText:'请选人员',
//												       selectOnFocus:true,
//													   forceSelection: true,
//													   editable:false,
//													   queryMode: 'local',
//												       labelAlign :"right",
//												       id:'employee_12',
//												       fieldLabel: '人员',
//												       name: 'employee',
//												       store :"EmployeesStore",
//												       displayField: 'name',
//			    									   valueField: 'id'
//								   			}]
//					        		}]	
//					        	},
					        		{
							         layout : "column",
							         border : false,			 
								     items: [{
									        	columnWidth :.45,
												layout : "form",
												border : false,
									   			items: [{
										   				    xtype : 'datefield',
										   				    labelAlign :"right",
										   				    fieldLabel: '开始时间',
												            name: 'starttime',
												            format:'Y-m-d'
											        }]		        	
								        	},{
								        		columnWidth :.45,
												layout : "form",
												border : false,
									   			items: [{
										   				    xtype : 'datefield',
										   				    labelAlign :"right",
										   				    fieldLabel: '结束时间',
												            name: 'endtime',
												            format:'Y-m-d'
												            
											        }]	
								        		
								        	}]
							  },{
					   		 	layout : "column",
							 	border : false,
									items: [{
											 columnWidth :.80,
									 		 layout : "form",
											 xtype : "textarea",
									      	 labelAlign :"right",
									         fieldLabel: '加班内容',
									         name: 'content'
									},{
										     columnWidth :.10,
									 		 layout : "form",
											 xtype : "textfield",
									      	 labelAlign :"right",
									         fieldLabel: 'ID',
									         name: 'id',
									         hidden:true
									}]
			   		 }]	        	
		
		 })
			var win = new Ext.Window({
				title : "修改",
				width : 700,
				height : 300,
				layout:'fit',
				frame:true,
				items : [ myform1
							],
				buttons : [{
							text : "确定",
							handler : function() {
								win.getComponent(0).getForm().submit({
											 url : 'AddWork_editAddWork',
											 success : function() {
												Ext.Msg.alert('提示', '修改成功');
												win.close();
												grid.getStore().reload();
												grid.getSelectionModel().deselectAll();
											},
											failure : function() {
												Ext.Msg.alert('错误', '修改失败');
											},
											waitMsg : "请稍后，正在保存。。。。"
	
										});
							}
						}, {
							text : "重置",
							handler : function() {
								win.getComponent(0).getForm().reset();
							}
						}, {
							text : "取消",
							handler : function() {
								win.close();
											
							}
						}]
			});
		var grid=this.ownerCt.ownerCt;
		var selections = grid.getSelectionModel().getSelection();
						if(selections.length==0){
							Ext.MessageBox.alert("提示","请选择要编辑的一行！");
						}else if (selections.length > 1) {
							Ext.MessageBox.alert("提示", "请检查您是否选中了多行！");
							return;
						} else{
							var record = selections[0];
						//这里是修改操作的实现部分
							win.getComponent(0).getForm().loadRecord(record);
							win.show();
						}
	}

	function deleteWork(){
				 var grid=this.ownerCt.ownerCt;
			     var selections = grid.getSelectionModel().getSelection();
					if(selections){
					if(selections.length==0){
							Ext.MessageBox.alert("提示","请检查是否选中某一行！");
						}else if (selections.length > 1) {
							Ext.MessageBox.alert("提示", "请检查您是否选中了多行！");
							return;
						} else{
							Ext.MessageBox.confirm("提示", "确认要删除 ？", function(btn) {
							if (btn == 'yes') {			
							var record = selections[0];
								Ext.Ajax.request({
										url : 'AddWork_deleteAddWork',
										success : function() {
											Ext.MessageBox.alert("提示",	"删除成功！");
													 grid.getStore().remove(selections );
												     grid.getStore().reload();
										},
										failure : function() {
											Ext.MessageBox.alert("提示", "删除失败！");
										},
										params : {
											id : record.get("id")
										}
									});			
								}
					  	})
                   	} 
 			   
		   }				
	}